﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using PT.ThueXe;

namespace PT.CarList
{
    partial class ViewCarList : PortalModuleBase, IActionable
    {

        #region Private Members
        public CarListController _objCarController;
        #endregion

        #region Public Methods

        public CarListController objCarController
        {
            get
            {
                if (this._objCarController == null)
                {
                    this._objCarController = new CarListController();
                }
                return this._objCarController;
            }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["datxe"]) && !string.IsNullOrEmpty(Request.QueryString["CarID"]))
                    {
                        ThueXeController objController = new ThueXeController();
                        List<CityInfo> cityList = objController.GetCityList();
                        this.cboFromCity.DataSource = cityList;
                        this.cboFromCity.DataBind();

                        this.cboToCity.DataSource = cityList;
                        this.cboToCity.DataBind();

                        this.mvView.SetActiveView(this.viewDatXe);
                    }
                    else if (!string.IsNullOrEmpty(Request.QueryString["CarID"]))
                    {
                        CarInfo objCar = this.objCarController.GetCar(int.Parse(Request.QueryString["CarID"]));

                        this.lblName.Text = objCar.CarName;
                        this.lblManufacture.Text = objCar.CarManufacture;
                        this.lblCarType.Text = objCar.CarType;
                        this.lblPrice.Text = objCar.Price;
                        this.lblDetailInfo.Text = Server.HtmlDecode(objCar.DetailInfo);
                        this.mvView.SetActiveView(this.viewDetail);
                    }
                    else
                    {
                        List<CarInfo> carList = this.objCarController.GetCarList(this.ModuleId);
                        foreach (CarInfo item in carList)
                        {
                            item.OtherInfo = Server.HtmlDecode(item.OtherInfo);
                        }
                        this.lvCarList.DataSource = carList;
                        this.lvCarList.DataBind();
                    }

                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void _btnBack_Click(Object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.TabId, "", ""));
        }

        protected void _btnBackToCar_Click(Object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.TabId, "", "CarID=" + Request.QueryString["CarID"].ToString()));
        }
        protected void _btnDatXe_Click(Object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.TabId, "", "CarID=" + Request.QueryString["CarID"].ToString() + "&datxe=1"));
        }

        protected void _btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                ThueXeInfo objThueXe = new ThueXeInfo();
                objThueXe.ModuleId = this.ModuleId;
                objThueXe.ItemId = 0;
                objThueXe.Name = this.txtName.Text;
                objThueXe.Address = this.txtAddress.Text;
                objThueXe.Phone = this.txtPhone.Text;
                objThueXe.Email = this.txtEmail.Text;
                objThueXe.CarTypeID = 1;
                objThueXe.FromCityID = int.Parse(this.cboFromCity.SelectedValue);
                objThueXe.ToCityID = int.Parse(this.cboToCity.SelectedValue);
                objThueXe.FromDate = this.dteFromDate.SelectedDate.Value;
                objThueXe.ToDate = this.dteTodate.SelectedDate.Value;
                objThueXe.OtherInformation = this.txtOtherInfo.Text;
                objThueXe.CreatedDate = DateTime.Now;
                string strXmlDetail = @"<root>" +
                   "<item itemid='{0}' tenxe='{1}' hieuxe='{2}' loaixe='{3}' />" +
                   "</root>";
                CarInfo objCar = this.objCarController.GetCar(int.Parse(Request.QueryString["CarID"]));
                objThueXe.DetailInformation = string.Format(strXmlDetail, Request.QueryString["CarID"], objCar.CarName, objCar.CarManufacture, objCar.CarType);
                ThueXeController objController = new ThueXeController();
                objController.AddUpdateThueXe(objThueXe);
                try
                {
                    ThueXeEmail objEmail = objController.GetThueXeEmail();
                    if (objEmail != null && !string.IsNullOrEmpty(objEmail.MailFrom.Trim()) && !string.IsNullOrEmpty(objEmail.MailTo.Trim()))
                    {
                        string smtpServer = Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPServer"]);
                        string smtpAuthentication = Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPAuthentication"]);
                        string smtpUsername = Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPUsername"]);
                        string smtpPassword = Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPPassword"]);

                        string xResult = DotNetNuke.Services.Mail.Mail.SendMail(
                            objEmail.MailFrom.Trim(),
                            objEmail.MailTo.Trim(),
                            "",
                            "",
                            DotNetNuke.Services.Mail.MailPriority.Normal,
                            Localization.GetString("Subject", this.LocalResourceFile),
                            DotNetNuke.Services.Mail.MailFormat.Text,
                            System.Text.Encoding.UTF8,
                            "Thông tin đăng ký thuê xe ngày" + DateTime.Now.ToString() + "<br/>" +
                            "Họ và tên:" + this.txtName.Text + "<br/>" +
                            "Địa chỉ:" + this.txtAddress.Text + "<br/>" +
                            "Điện thoại:" + this.txtPhone.Text + "<br/>" +
                            "Email:" + this.txtEmail.Text + "<br/>" +
                            "Tên xe:" + objCar.CarName + "<br/>" +
                            "Hiệu xe:" + objCar.CarManufacture + "<br/>" +
                            "Loại xe:" + objCar.CarType + "<br/>" +
                            "Từ nơi:" + this.cboFromCity.SelectedItem.Text + "<br/>" +
                            "Đến nơi" + this.cboToCity.SelectedItem.Text + "<br/>" +
                            "Từ ngày:" + this.dteFromDate.SelectedDate.Value.ToString("dd/MM/yyyy") + "<br/>" +
                            "Đến ngày:" + this.dteTodate.SelectedDate.Value.ToString("dd/MM/yyyy") + "<br/>" +
                            "Thông tin khác:" + this.txtOtherInfo.Text.Replace("\r\n", "<br />").Replace("\n", "<br />").Replace("\r", "<br />") + "<br/>",
                            "",
                            smtpServer,
                            smtpAuthentication,
                            smtpUsername,
                            smtpPassword);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "ShowMessage", "alert('Thông tin đã được gửi đi. Cảm ơn bạn đã liên hệ. Chúng tôi sẽ liên lạc với bạn sau.');", true);
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(this.GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile), ModuleActionType.AddContent, "", "", this.EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

    }
}

